package com.tungkong.usbcamera.view;

import android.hardware.usb.UsbDevice;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.tungkong.usbcamera.R;
import com.tungkong.usbcamera.application.ClientApplication;
import com.tungkong.usbcamera.UVCCameraPreview;

import java.io.File;

/**
 * 创建日期：2021-07-16 15:45
 * 创建者：吴开杰
 * 修改日期：2021-07-16 15:45
 * 修改者：吴开杰
 * 版本：v.1.0
 * 描述：
 * 1.
 * 2.
 * 3.
 */
public class USBSingleRecordVideoActivity extends AppCompatActivity {

    private String TAG = "USBSingleRecordVideoActivity";

    private UVCCameraPreview mCameraHelper;

    private UsbDevice usbDevice;
    private TextView mMsgTV;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_record_video_single);

        mMsgTV = this.findViewById(R.id.msg);

        mCameraHelper = UVCCameraPreview.getInstance();
        mCameraHelper.setDefaultFrameFormat(UVCCameraPreview.FRAME_FORMAT_MJPEG);
        mCameraHelper.initUSBMonitor(this);

    }

    @Override
    protected void onStart() {
        super.onStart();
        if (mCameraHelper != null) {
            mCameraHelper.registerUSB();
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
        if (mCameraHelper != null) {
            mCameraHelper.unregisterUSB();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mCameraHelper != null){
            mCameraHelper.release(usbDevice);
        }
    }

    public void onDisplayUsbDevices(View view) {

        // 列出全部的usb设备
        Log.e("onDisplayUsbDevices","-------------列出全部usb设备");
        for (UsbDevice usbDevice: mCameraHelper.getUsbDeviceList()) {
            Log.e("onDisplayUsbDevices","vid:" + usbDevice.getVendorId() + ",pid:" + usbDevice.getProductId());
        }

        // 列出连接的usb设备
        StringBuffer buffer = new StringBuffer();
        buffer.append("枚举摄像头: ");
        buffer.append("\n\n");
        Log.e("onDisplayUsbDevices","-------------列出连接usb设备");
        for (UsbDevice usbDevice: mCameraHelper.getConnectedUsbDevices()) {
            Log.e("onDisplayUsbDevices","vid:" + usbDevice.getVendorId() + ",pid:" + usbDevice.getProductId());
            buffer.append("vid:");
            buffer.append(usbDevice.getVendorId());
            buffer.append("，pid:");
            buffer.append(usbDevice.getProductId());
            buffer.append("\n");
        }

        mMsgTV.setText(buffer.toString());
    }

    public void onOpenUsbDevice(View view){
        Log.i(TAG, "打开USB摄像头");
        if (mCameraHelper == null) {
            Log.i(TAG, "请初始化USB摄像头");
            return;
        }

//        // 前置
//        usbDevice1 = mCameraHelper.getUsbDevice(5546,5461);
//        // 票仓
//        usbDevice2 = mCameraHelper.getUsbDevice(21862,4660);

        usbDevice = mCameraHelper.getUsbDevice(21862,4660);
        if(usbDevice != null){
            mCameraHelper.openCamera(usbDevice);
        } else {
            Log.i(TAG, "该USB摄像头未连接");
        }

        mMsgTV.setText("摄像头已打开!");
    }

    // 注意：录制摄像头之前，需要打开usb摄像头，打开时间可能需要300ms，之后才可以录制
    public void onStartPusher(View view){
        Log.i(TAG, "开始录制");
        if (mCameraHelper == null) {
            Log.i(TAG, "请初始化USB摄像头");
            return;
        }
        Log.i(TAG, "录制状态：正在录制");
        // 文件路径
//        String videoPath = getFileName(usbDevice);
        // 开始录制
        mCameraHelper.startPusher(usbDevice);
        mMsgTV.setText("录制状态：正在录制");
    }

    public void onStopPusher(View view){
        Log.i(TAG, "结束录制");
        if (mCameraHelper == null) {
            Log.i(TAG, "请初始化USB摄像头");
            return;
        }
        Log.i(TAG, "录制状态：停止");
        mCameraHelper.stopPusher(usbDevice);
        mMsgTV.setText("录制状态：停止");
    }

    public void onStartPreview(View view){
        Log.i(TAG, "打开数据流");
        if (mCameraHelper == null) {
            Log.i(TAG, "请初始化USB摄像头");
            return;
        }
        mCameraHelper.startBackgroundPreview(usbDevice);
    }

    public void onStopPreview(View view){
        Log.i(TAG, "关闭数据流");
        if (mCameraHelper == null) {
            Log.i(TAG, "请初始化USB摄像头");
            return;
        }
        mCameraHelper.stopPreview(usbDevice);
    }

    public void onCaptureCut(View view){
        Log.i(TAG, "截取图像");
        if (mCameraHelper == null) {
            Log.i(TAG, "请初始化USB摄像头");
            return;
        }
        String picPath = UVCCameraPreview.ROOT_PATH + ClientApplication.DIRECTORY_NAME + File.separator + System.currentTimeMillis() + UVCCameraPreview.SUFFIX_JPEG;
        Log.i(TAG, "图像地址：" + picPath);
        mCameraHelper.captureCut(usbDevice, picPath);
    }


}
